package com.migu.contacts.main.my;

import static com.migu.contacts.net.Service.API;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.InvitationBean;
import com.migu.contacts.bean.Page;
import com.migu.contacts.databinding.ActivityInviteBinding;
import com.migu.contacts.databinding.ItemInviteBinding;
import com.migu.contacts.databinding.PopInviteBinding;
import com.migu.contacts.net.ResultCallback;
import com.migu.contacts.util.QRCodeGenerator;
import com.migu.contacts.util.UserCenter;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.List;

public class InviteActivity extends BaseActivity<ActivityInviteBinding> {

    @Override
    protected void initView() {
        super.initView();
        mBinding.setBean(UserCenter.userBean);
        mBinding.code.setImageBitmap(QRCodeGenerator.generateQRCode(UserCenter.userBean.invitationCode, 300, 300));
    }

    @Override
    protected void initData() {
        super.initData();
        API.orderInvitationPage(1, 9999).enqueue(new ResultCallback<Page<InvitationBean>>() {
            @Override
            protected void onSuccess(Page<InvitationBean> invitationBeanPage) {
                if (invitationBeanPage != null) {
                    List<InvitationBean> list = invitationBeanPage.list;
                    baseAdapter.setData(list);
                }
            }
        });
    }

    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemInviteBinding, InvitationBean> baseAdapter = new BaseAdapter<ItemInviteBinding, InvitationBean>() {
    };

    public void invite(View view) {
        CenterPopup bottomPopup = new CenterPopup<PopInviteBinding>(mActivity) {

            @Override
            protected void initView() {
                super.initView();
                mBinding.setBean(UserCenter.userBean);
                mBinding.code.setImageBitmap(QRCodeGenerator.generateQRCode(UserCenter.userBean.invitationCode, 300, 300));
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("label", UserCenter.userBean.invitationCode);
                clipboard.setPrimaryClip(clip);
                dismiss();
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
        UMImage image = new UMImage(mActivity, "Constants.ImageUrl + rsp.getShare_pic()");
        UMWeb web = new UMWeb("rsp.getShare_url()");
        web.setTitle("rsp.getShare_title()");//标题
        web.setThumb(image);//缩略图
        web.setDescription("rsp.getShare_desc()");//描述
        new ShareAction(mActivity).withMedia(web).setCallback(new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Log.d(TAG, "onStart() called with: share_media = [" + share_media + "]");
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                Log.d(TAG, "onResult() called with: share_media = [" + share_media + "]");
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                Log.d(TAG, "onError() called with: share_media = [" + share_media + "], throwable = [" + throwable + "]");
//                ToastUtil.showShort("分享失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                Log.d(TAG, "onCancel() called with: share_media = [" + share_media + "]");
            }
        }).share();
    }

    public void copy(View view) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", UserCenter.userBean.invitationCode);
        clipboard.setPrimaryClip(clip);
    }
}